package jndi;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class TestJNDI {

	public static void main(String[] args) {
		try {
			// 初始化查找命名空间
			Context ctx = new InitialContext();
			// InitialContext ctx = new InitialContext();亦可
			// 找到DataSource,对名称进行定位java:comp/env是必须加的,后面跟你的DataSource名
			DataSource ds = null;
			ds = (DataSource) ctx.lookup("java:comp/env/jdbc/birtdb");
			// 取出连接
			Connection conn = ds.getConnection();
			PreparedStatement prepareStatement = conn.prepareStatement("");
			ResultSet rs = prepareStatement.executeQuery();
			System.out.println("connection pool connected ! " + rs.getRow());
		} catch (NamingException e) {
			System.out.println(e.getMessage());
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}
